package com.tys.algorithm.advanced.code.class03;

import java.util.Stack;

/**
 * 获得栈中最小值
 * 方法1：最小栈和数据栈不同步增长
 * 方法2：最小栈和数据栈，同步增长
 */
public class Code05_GetMinStack {
    //方法1：最小栈和数据栈不同步增长
    //<=时才入栈
    public static class MyStack1 {
        //定义数据栈和最小栈
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;

        public MyStack1() {
            this.stackData = new Stack<Integer>();
            this.stackMin = new Stack<Integer>();
        }

        //入栈
        public void push(int value) {
            if (this.stackMin.isEmpty()) {
                //最小栈为空入栈
                this.stackMin.push(value);
            } else if (value <= this.getmin()) {
                //<=最小栈栈顶时入栈
                this.stackMin.push(value);
            }
            //入数据栈
            this.stackData.push(value);
        }

        //出栈
        public int pop() {
            //为空抛异常
            if (this.stackData.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            //数据栈栈顶
            int value = this.stackData.pop();
            //最小栈栈顶
            if (value == this.getmin()) {
                //相等则弹出
                this.stackMin.pop();
            }
            //返回数据栈顶值
            return value;
        }


        //获取最小栈顶值
        public int getmin() {
            //最小栈为空抛异常
            if (this.stackMin.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            //返回最小栈栈顶
            return this.stackMin.peek();
        }
    }

    //方法2：最小栈和数据栈，同步增长
    //>=时取最小栈栈顶重复入最小栈，<时直接入最小栈
    public static class MyStack2 {
        //定义数据栈和最小栈
        private Stack<Integer> stackData;
        private Stack<Integer> stackMin;

        public MyStack2() {
            this.stackData = new Stack<Integer>();
            this.stackMin = new Stack<Integer>();
        }

        //入栈
        public void push(int value) {
            if (this.stackMin.isEmpty()) {
                //最小栈为空直接入栈
                this.stackMin.push(value);
            } else if (value < this.getmin()) {
                //<直接入最小栈
                this.stackMin.push(value);
            } else {
                //>=取栈顶入最小栈
                stackMin.push(getmin());
            }
            //入数据栈
            this.stackData.push(value);
        }

        //出栈
        public int pop() {
            //为空抛异常
            if (this.stackData.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            //最小栈弹出栈顶
            this.stackMin.pop();
            //数据栈弹出栈顶
            return this.stackData.pop();
        }

        //获取最小栈栈顶值
        public int getmin() {
            //最小栈为空抛异常
            if (this.stackMin.isEmpty()) {
                throw new RuntimeException("Your stack is empty.");
            }
            //返回最小栈栈顶
            return this.stackMin.peek();
        }
    }

    public static void main(String[] args) {
//        Stack stack = new Stack();
//        System.out.println(stack.peek());
        MyStack1 stack1 = new MyStack1();
        stack1.push(3);
        System.out.println(3 == stack1.getmin());
        stack1.push(3);
        System.out.println(3 == stack1.getmin());
        stack1.push(4);
        System.out.println(3 == stack1.getmin());
        stack1.push(1);
        System.out.println(1 == stack1.getmin());
        stack1.push(1);
        System.out.println(1 == stack1.getmin());
        System.out.println(1 == stack1.pop());
        System.out.println(1 == stack1.getmin());
        System.out.println("================");

        MyStack2 stack2 = new MyStack2();
        stack2.push(3);
        System.out.println(3 == stack2.getmin());
        stack2.push(4);
        System.out.println(3 == stack2.getmin());
        stack2.push(1);
        System.out.println(1 == stack2.getmin());
        stack2.push(1);
        System.out.println(1 == stack2.getmin());
        System.out.println(1 == stack2.pop());
        System.out.println(1 == stack2.getmin());

    }

}
